﻿using System;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Web;

namespace Shumisen.Web.Form.Modules
{
	public class TraceModule : IHttpModule
	{
		public void Init(HttpApplication context)
		{
			{
				Debug.WriteLine("-----------------------------------------------");
			}
			context.BeginRequest += OnEvent;
			context.AuthenticateRequest += OnEvent;
			context.PostAuthenticateRequest += OnEvent;
			context.AuthorizeRequest += OnEvent;
			context.PostAuthorizeRequest += OnEvent;
			context.ResolveRequestCache += OnEvent;
			context.PostResolveRequestCache += OnEvent;
			context.MapRequestHandler += OnEvent;
			context.PostMapRequestHandler += OnEvent;
			context.AcquireRequestState += OnEvent;
			context.PostAcquireRequestState += OnEvent;
			context.PreRequestHandlerExecute += OnEvent;
			context.PostRequestHandlerExecute += OnEvent;
			context.ReleaseRequestState += OnEvent;
			context.PostReleaseRequestState += OnEvent;
			context.UpdateRequestCache += OnEvent;
			context.PostUpdateRequestCache += OnEvent;
			context.LogRequest += OnEvent;
			context.PostLogRequest += OnEvent;
			context.EndRequest += OnEvent;
			context.PreSendRequestHeaders += OnEvent;
			context.PreSendRequestContent += OnEvent;
		}

		private void OnEvent(object sender, EventArgs e)
		{
			var context = HttpContext.Current;
			if (context.IsPostNotification)
			{
				Debug.WriteLine(string.Format("TraceModule[{0}]: Post {1}: {2}", Thread.CurrentThread.ManagedThreadId, context.CurrentNotification, HttpContext.Current.Request.CurrentExecutionFilePath));
			}
			else
			{
				Debug.WriteLine(string.Format("TraceModule[{0}]: Pre  {1}: {2}", Thread.CurrentThread.ManagedThreadId, context.CurrentNotification, HttpContext.Current.Request.CurrentExecutionFilePath));
			}
			if (context.CurrentNotification == RequestNotification.LogRequest)
			{
				var req = HttpContext.Current.Request;
				var buf = new StringBuilder();
				buf.Append("AnonymousID=").Append(req.AnonymousID).Append(Environment.NewLine);
				buf.Append("ApplicationPath=").Append(req.ApplicationPath).Append(Environment.NewLine);
				buf.Append("AppRelativeCurrentExecutionFilePath=").Append(req.AppRelativeCurrentExecutionFilePath).Append(Environment.NewLine);
				buf.Append("CurrentExecutionFilePath=").Append(req.CurrentExecutionFilePath).Append(Environment.NewLine);
				buf.Append("CurrentExecutionFilePathExtension=").Append(req.CurrentExecutionFilePathExtension).Append(Environment.NewLine);
				buf.Append("FilePath=").Append(req.FilePath).Append(Environment.NewLine);
				buf.Append("Path=").Append(req.Path).Append(Environment.NewLine);
				buf.Append("PathInfo=").Append(req.PathInfo).Append(Environment.NewLine);
				buf.Append("PhysicalPath=").Append(req.PhysicalPath).Append(Environment.NewLine);
				buf.Append("PhysicalApplicationPath=").Append(req.PhysicalApplicationPath).Append(Environment.NewLine);
				buf.Append("RawUrl=").Append(req.RawUrl).Append(Environment.NewLine);
				buf.Append("RequestType=").Append(req.RequestType).Append(Environment.NewLine);
				buf.Append("Url=").Append(req.Url).Append(Environment.NewLine);
				buf.Append("UserHostAddress=").Append(req.UserHostAddress).Append(Environment.NewLine);
				buf.Append("UserHostName=").Append(req.UserHostName).Append(Environment.NewLine);
				//Debug.Write(buf);
			}
		}

		public void Dispose()
		{
		}
	}
}